package com.hbisedm.interninfosys.intern.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbisedm.interninfosys.commonutils.R;
import com.hbisedm.interninfosys.intern.entity.User;
import com.hbisedm.interninfosys.intern.entity.UserEvaluate;
import com.hbisedm.interninfosys.intern.entity.UserEvaluateVO;
import com.hbisedm.interninfosys.intern.entity.vo.EvaluateVo;
import com.hbisedm.interninfosys.intern.service.IUserEvaluateService;
import com.hbisedm.interninfosys.intern.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  用户评价
 * </p>
 *
 * @author Sam
 * @since 2020-12-03
 */
@RestController
@RequestMapping("/intern/user-evaluate")
public class UserEvaluateController {
    @Autowired
    IUserEvaluateService userEvaluateService;
    @Autowired
    IUserService userService;

    @PostMapping("/list/{current}/{limit}")
    public R evaluateList(@PathVariable long current,
                          @PathVariable long limit,
                          @RequestBody(required = false) UserEvaluate userEvaluate) {
        /**
         * todo 不同Type类型
         */
        QueryWrapper<UserEvaluate> userEvaluateQueryWrapper = new QueryWrapper<>();
        ArrayList<UserEvaluateVO> userEvaluateVOS = new ArrayList<>();
        Page<UserEvaluate> userEvaluatePage = new Page<>(current, limit);
        userEvaluateService.page(userEvaluatePage, userEvaluateQueryWrapper);
        List<UserEvaluate> list = userEvaluatePage.getRecords();
        long total = userEvaluatePage.getTotal();
        for (UserEvaluate evaluate : list) {
            if(evaluate.getUid() != null && evaluate.getTargetId() != null) {
                UserEvaluateVO userEvaluateVO = new UserEvaluateVO();
                userEvaluateVO.setId(evaluate.getId());
                User uUser = userService.getById(evaluate.getUid());
                userEvaluateVO.setUid(uUser.getId());
                userEvaluateVO.setUname(uUser.getOthername());
                userEvaluateVO.setUType(uUser.getType());
                User targetUser = userService.getById(evaluate.getTargetId());
                userEvaluateVO.setTargetId(targetUser.getId());
                userEvaluateVO.setTargetName(targetUser.getOthername());
                userEvaluateVO.setTargetType(targetUser.getType());

                userEvaluateVO.setEvaluate(evaluate.getEvaluate());
                userEvaluateVO.setCreateTime(evaluate.getCreateTime());
                userEvaluateVO.setUpdateTime(evaluate.getUpdateTime());
                userEvaluateVOS.add(userEvaluateVO);
            }
        }

        return R.ok().data("userEvaluates", userEvaluateVOS).data("total", total);
    }

    /**
     * 通过targetId获取其所对应的评价列表
     * @param targetId
     * @return
     */
    @GetMapping("/list/{targetId}")
    public R listByTargetId(@PathVariable int targetId) {
        List<UserEvaluate> list = userEvaluateService.list(new QueryWrapper<UserEvaluate>().lambda().eq(UserEvaluate::getTargetId, targetId));
        ArrayList<EvaluateVo> result = new ArrayList<EvaluateVo>();
        list.stream().forEach(item -> {
            User byId = userService.getById(item.getUid());
            String othername = byId.getOthername();
            String evaluate = item.getEvaluate();
            LocalDateTime updateTime = item.getUpdateTime();
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm");
            String format = updateTime.format(dtf);
            EvaluateVo evaluateVo = new EvaluateVo(othername, evaluate, format);
            result.add(evaluateVo);
        });
        return R.ok().data("list", result);
    }

    /**
     * 管理界面编辑rowId获取当前row的评价信息
     * @param id
     * @return
     */
    @PostMapping("/info/{id}")
    public R evaluateInfo(@PathVariable Integer id) {
        UserEvaluate currentEvaluate = userEvaluateService.getById(id);
        UserEvaluateVO userEvaluateVO = new UserEvaluateVO();
        BeanUtils.copyProperties(currentEvaluate, userEvaluateVO);
        User uUser = userService.getById(currentEvaluate.getUid());
        userEvaluateVO.setUname(uUser.getOthername());
        userEvaluateVO.setUType(uUser.getType());
        User targetUser = userService.getById(currentEvaluate.getTargetId());
        userEvaluateVO.setTargetName(targetUser.getOthername());
        userEvaluateVO.setTargetType(targetUser.getType());

        return R.ok().data("evaluate", userEvaluateVO);
    }


    /**
     * 查询教师的评价
     */
    @PostMapping("/infoBySid/{sid}/{tid}")
    public R evaluateInfoBySid(@PathVariable Integer sid, @PathVariable Integer tid) {
        UserEvaluate one = userEvaluateService.getOne(new QueryWrapper<UserEvaluate>().lambda().eq(UserEvaluate::getUid, sid).eq(UserEvaluate::getTargetId, tid));

        EvaluateVo userEvaluateVO = new EvaluateVo();
        if(one != null) {
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm");
            String format = one.getUpdateTime().format(dtf);
            userEvaluateVO.setEvaluateTime(format);
            userEvaluateVO.setEvaluate(one.getEvaluate());
            userEvaluateVO.setUsername(userService.getById(sid).getOthername());
        }

        return R.ok().data("evaluate", userEvaluateVO);
    }

    @PutMapping("/update")
    public R evaluateUpdate(@RequestBody UserEvaluate evaluate) {
        boolean b = userEvaluateService.saveOrUpdate(evaluate);
        if(b) {
            return R.ok();
        }else {
            return R.error();
        }
    }

    @DeleteMapping("/delete/{id}")
    public R evaluateDelete(@PathVariable Integer id) {
        boolean b = userEvaluateService.removeById(id);
        if(b) {
            return R.ok();
        }else {
            return R.error();
        }
    }

    @PostMapping("/add")
    public R evaluateAdd(@RequestBody UserEvaluate evaluate) {
        boolean b = userEvaluateService.saveOrUpdate(evaluate);
        if(b) {
            return R.ok();
        }else {
            return R.error();
        }
    }

    @PostMapping("/getEvaluateShow")
    public R getEvaluateShow(@RequestBody UserEvaluate userEvaluate) {
        Integer iid = userEvaluate.getUid();
        Integer sid = userEvaluate.getTargetId();
        UserEvaluate one = userEvaluateService.getOne(new QueryWrapper<UserEvaluate>().lambda().eq(UserEvaluate::getUid, iid).eq(UserEvaluate::getTargetId, sid));
        if(one == null) {
            return R.ok().data("show", false);
        }else {
            return R.ok().data("show", true);
        }

    }

}
